import io.codis.jodis.JedisResourcePool;
import io.codis.jodis.;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.util.HashSet;
import java.util.Set;


/**
 * Created by luoqifei on 17-4-17.
 */
public class RedisClientTest {
    private Jedis jedis;
    private static String cpx="fieldGroupIdValue_idField1Name-idField1Value_idField2Name-idField2Value";
    private static String fieldSample = "23.761232%s%s,";
    private static long timestamp=1493104167000L;
    private static int counter = 0;
    public RedisClientTest(String redisIp,int port){
        jedis = new Jedis(redisIp,port,99999999);
    }
    public void flushDb(){
        jedis.flushDB();
    }
    public void flushAll(){
        jedis.flushAll();
    }
    public boolean setRecord(long number, String record,String asset){
        for(long i=0;i<number;i++){
            jedis.zadd(asset,timestamp+i,record+i);
            System.out.println(i);
        }
        return true;
    }
    public boolean setRecord2(long number, String record,String asset){
        String b = jedis.get(asset);
        if(b == null){
            b="1";
        }
        for(long i=0;i<number;i++){
            jedis.zadd("idx::"+asset,timestamp++,b);
            jedis.hset("val::"+asset,b,record+i);
            jedis.hset("time::"+asset,""+System.currentTimeMillis()/1000,b);
            jedis.incr(asset);
            b=jedis.get(asset);
            counter=counter+5;
        }
        return true;
    }
    public static void removeData(){
        Jedis jedis = new Jedis("127.0.0.1",2999,60000);
        //each assert had 48000 record
        long start1=System.currentTimeMillis();
        for(int a=0;a<20;a++){
            jedis.zremrangeByRank("idx::fieldGroupIdValue_idField1Name-idField1Value_idField2Name-idField2Value"+a,0,1000);
        }
        System.out.println("del sort set,"+(System.currentTimeMillis()-start1)+" ms.");
        StringBuilder fields = new StringBuilder();
        for(int k=0;k<110000;k++){
            fields.append(k);
            if(k!=110000){
                fields.append(",");
            }
        }
        long start2=System.currentTimeMillis();
        for(int a=0;a<20;a++){
            jedis.hdel("val::fieldGroupIdValue_idField1Name-idField1Value_idField2Name-idField2Value" + a,fields.toString().split(",") );
        }
        System.out.println("del hash,"+(System.currentTimeMillis()-start2)+" ms.");

        long start3=System.currentTimeMillis();
        for(int a=0;a<20;a++){
            String aa[] =jedis.hkeys("time::fieldGroupIdValue_idField1Name-idField1Value_idField2Name-idField2Value"+a).toArray(new String[]{});
            jedis.hdel("time::fieldGroupIdValue_idField1Name-idField1Value_idField2Name-idField2Value" + a,aa );
        }
        System.out.println("del hash time,"+(System.currentTimeMillis()-start3)+" ms.");
    }
    public Set<String> getSortSet(String key){
        return  jedis.zrange(key,0,-1);
    }
    public static void main(String args[]){
        long time=System.currentTimeMillis();
        set(1);
        System.out.println(counter);
        System.out.println((System.currentTimeMillis()-time)/1000+" seconds");
        //flush();
        //removeData();
        //Jedis jedis = new Jedis("127.0.0.1",2999,60000);

    }
    public static void set(int minutes){
        RedisClientTest test = new RedisClientTest("127.0.0.1",2999);
        StringBuilder stringBuilder = new StringBuilder();
        int fieldNum = 47;
        long seconds = 60*minutes;
        long writeSpeed = 200;
        int assetNum = 60;
        for(int i=0;i<fieldNum;i++){
            stringBuilder.append(String.format(fieldSample,String.valueOf(i+1),String.valueOf(i+1)));
        }
        int maxInt = Integer.MAX_VALUE;
        stringBuilder.append(maxInt+","+maxInt+","+maxInt+",");
        stringBuilder.append("123456");
        //test.flushDb();

        for(int k=0;k<assetNum;k++){
            test.setRecord2(seconds*writeSpeed,stringBuilder.toString(),cpx+k);
        }
        long recordSize = stringBuilder.toString().getBytes().length;
        System.out.println("record size is : "+(recordSize+cpx.getBytes().length)+" Byte.");
        System.out.println("should had send "+seconds*writeSpeed*assetNum+" records.");
        System.out.println("memory should add : "+seconds*writeSpeed*assetNum*recordSize/1024l+" kb.");
    }
    public static void flush(){
        Jedis jedis = new Jedis("127.0.0.1",2999,60000);
        jedis.flushDB();
//        JedisPoolConfig config = new JedisPoolConfig();
//        config.setMaxTotal(100);
//        config.setMaxIdle(50);//max aviarable connection
//        config.setMinIdle(10);//min aviarable connection
//        config.setTestOnBorrow(true);
//        config.setMaxWaitMillis(6*1000);//6 seconds
//        Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
//        jedisClusterNodes.add(new HostAndPort("127.0.0.1",6381));
//        JedisCluster jedisCluster = new JedisCluster(jedisClusterNodes,2000,100,config);
          //  jedis.set("joe000","ddd");
       // System.out.println(jedis.get("joe000"));
       // jedis.hset("fieldGroupIdValue-idField1Name-idField1Value_idField2Name-idField2Value0y","100","fieldIdName1:fieldIdValue1,fieldIdName2:fieldIdValue2,fieldIdName3:fieldIdValue3,");
        //jodis
//        JedisResourcePool jedisPool = RoundRobinJedisPool.create().curatorClient("127.0.0.1:2181", 30000).zkProxyDir("").build();
//        Jedis jedis1 = jedisPool.getResource();
//        jedis1.set("foo", "bar");
//            String value = jedis1.get("foo");
//            System.out.println(value);

    }
}
